#!/bin/bash
hname=$1
smtp_email=$2
postpass=$3
admin_email=$4
admin_pass=$5
admin_acct_name=$6
lms_stat_coll=$7
smtp_type=$8
smtp_port=$9
smtp_pass=${10}
echo "sudo update"
sudo apt-get --assume-yes update
echo "Postgres"
sudo apt-get --assume-yes install postgresql-9.5
sudo -u postgres createdb canvas_production --owner=postgres
echo "Changing the postgres user password"
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '$postpass';"
echo "git install"
sudo apt-get install git-core
echo "clone canvas"
git clone https://github.com/instructure/canvas-lms.git canvas
cd canvas
git checkout stable
git branch --set-upstream-to origin/stable
cd ../
echo "making canvas dir in var folder"
sudo mkdir -p /var/canvas
sudo chown -R $USER /var/canvas
cd canvas
echo "copying files"
cp -av . /var/canvas
cd /var/canvas
echo "External dependencies"
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:brightbox/ruby-ng
sudo apt-get --assume-yes update
echo "Ruby installation"
sudo apt-get --assume-yes install ruby2.4 ruby2.4-dev zlib1g-dev libxml2-dev \
                       libsqlite3-dev postgresql libpq-dev \
                       libxmlsec1-dev curl make g++
echo "Node JS"
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get --assume-yes install nodejs
echo "postgres create"
sudo -u postgres createuser $USER
sudo -u postgres psql -c "alter user $USER with superuser" postgres
echo "Bundler and Canvas dependencies"
sudo gem install bundler --version 1.13.6
bundle install --path vendor/bundle
echo "Yarn Installation"
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
sudo apt-get install python

echo "Yarn Installation"
yarn install
echo "Copying the config files for changing"
for config in amazon_s3 database \
  delayed_jobs domain file_store outgoing_mail security external_migration; \
  do cp config/$config.yml.example config/$config.yml; done
echo "Creating a dynamic setting file"
cp config/dynamic_settings.yml.example config/dynamic_settings.yml
#vim config/dynamic_settings.yml #Comment this line as we cant edit it.
echo "database file"
cp config/database.yml.example config/database.yml
sed -e "s/database: canvas_development/database: canvas_production/" config/database.yml > config/database_new.yml
mv config/database_new.yml config/database.yml
sed -e "s/username: canvas/username: postgres/" config/database.yml > config/database_new.yml
mv config/database_new.yml config/database.yml
sed -e "s/password: your_password/password: $postpass/" config/database.yml > config/database_new.yml
mv config/database_new.yml config/database.yml
echo "Outgoing email"
cp config/outgoing_mail.yml.example config/outgoing_mail.yml
sed -e "s/smtp.example.com/$smtp_type/" config/outgoing_mail.yml > config/outgoing_mail_new.yml
mv config/outgoing_mail_new.yml config/outgoing_mail.yml
sed -e "s/example.com/$hname/" config/outgoing_mail.yml > config/outgoing_mail_new.yml
mv config/outgoing_mail_new.yml config/outgoing_mail.yml
sed -e 's/"user"/"'"$smtp_email"'"/' config/outgoing_mail.yml > config/outgoing_mail_new.yml
mv config/outgoing_mail_new.yml config/outgoing_mail.yml
sed -e "s/25/$smtp_port/" config/outgoing_mail.yml > config/outgoing_mail_new.yml
mv config/outgoing_mail_new.yml config/outgoing_mail.yml
sed -e 's/"password"/"'"$smtp_pass"'"/' config/outgoing_mail.yml > config/outgoing_mail_new.yml
mv config/outgoing_mail_new.yml config/outgoing_mail.yml
echo "domain changes"
cp config/domain.yml.example config/domain.yml
sed -e "s/canvas.example.com/$hname/" config/domain.yml > config/domain_new.yml
mv config/domain_new.yml config/domain.yml
echo "Security Changes"
cp config/security.yml.example config/security.yml
sed -e "s/encryption_key: 12345/encryption_key: facdd3a131ddd8988b14f6e4e01039c93cfa0160/" config/security.yml > config/security_new.yml
mv config/security_new.yml config/security.yml
echo "Public Assests"
mkdir -p log tmp/pids public/assets app/stylesheets/brandable_css_brands
touch app/stylesheets/_brandable_variables_defaults_autogenerated.scss
touch Gemfile.lock
touch log/production.log
sudo chown -R $USER config/environment.rb log tmp public/assets \
                              app/stylesheets/_brandable_variables_defaults_autogenerated.scss \
                              app/stylesheets/brandable_css_brands Gemfile.lock config.ru
echo "Yarn Installation"
yarn install
cd /var/canvas
echo "Complie Assests"
sudo RAILS_ENV=production bundle exec rake canvas:compile_assets
sudo chown -R $USER public/dist/brandable_css
echo "loading initial database"
cd /var/canvas
RAILS_ENV=production bundle exec rake db:initial_setup
export CANVAS_LMS_ADMIN_EMAIL="$admin_email"
export CANVAS_LMS_ADMIN_PASSWORD="$admin_pass"
export CANVAS_LMS_ACCOUNT_NAME="$admin_acct_name"
export CANVAS_LMS_STATS_COLLECTION="$lms_stat_coll"
RAILS_ENV=production bundle exec rake db:initial_setup
sudo chown $USER config/*.yml
sudo chmod 400 config/*.yml
echo "Install Passenger"
sudo apt-get --assume-yes install passenger libapache2-mod-passenger apache2
sudo a2enmod rewrite
sudo a2enmod passenger
sudo a2enmod ssl
sudo service apache2 restart
echo "CANVAS CONF FILE"
cat > canvas.conf << EOF
<VirtualHost *:80>
  ServerName canvas.example.com
  ServerAlias files.canvas.example.com
  ServerAdmin youremail@example.com
  DocumentRoot /var/canvas/public
  RewriteEngine On
  RewriteCond %{HTTP:X-Forwarded-Proto} !=https
  RewriteCond %{REQUEST_URI} !^/health_check
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
  ErrorLog /var/log/apache2/canvas_errors.log
  LogLevel warn
  CustomLog /var/log/apache2/canvas_access.log combined
  SetEnv RAILS_ENV production
   <Directory /var/canvas/public>
    Options All
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>
<VirtualHost *:443>
  ServerName canvas.example.com
  ServerAlias files.canvas.example.com
  ServerAdmin youremail@example.com
  DocumentRoot /var/canvas/public
  ErrorLog /var/log/apache2/canvas_errors.log
  LogLevel warn
  CustomLog /var/log/apache2/canvas_ssl_access.log combined
  SSLEngine on
  BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
  BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
  # the following ssl certificate files are generated for you from the ssl-cert package.
  SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
  SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
  SetEnv RAILS_ENV production
   <Directory /var/canvas/public>
    Options All
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>
EOF
sed -e "s/canvas.example.com/$hname/" canvas.conf > canvas_new.conf
mv canvas_new.conf canvas.conf
sed -e "s/files.$hname/$hname/" canvas.conf > canvas_new.conf
mv canvas_new.conf canvas.conf
sed -e "s/youremail@example.com/$smtp_email/" canvas.conf > canvas_new.conf
mv canvas_new.conf canvas.conf
sudo mv canvas.conf /etc/apache2/sites-available/
sudo a2ensite canvas
echo "APACHE RESTART"
sudo service apache2 restart
cd /var/canvas
echo "INSTALL REDIS SERVER"
sudo apt-add-repository -y ppa:chris-lea/redis-server
sudo apt-get --assume-yes update
sudo apt-get --assume-yes install redis-server
sudo systemctl start redis
echo "CACHE STORE"
cd /var/canvas/
cp config/cache_store.yml.example config/cache_store.yml
sed -e '/development/ s/^#*/#/' -i config/cache_store.yml
cat >> config/cache_store.yml << EOF
 test:
    cache_store: redis_store
development:
    cache_store: redis_store
production:
    cache_store: redis_store
EOF
sudo chown $USER config/cache_store.yml
sudo chmod 400 config/cache_store.yml
echo "REDIS"
cd /var/canvas/
cp config/redis.yml.example config/redis.yml #redis localhost
cat >> config/redis.yml << EOF
production:
  servers:
    - redis://localhost
EOF
sudo chown $USER config/redis.yml
sudo chmod 400 config/redis.yml
echo "Canvas demon jobs"
sudo ln -s /var/canvas/script/canvas_init /etc/init.d/canvas_init
echo "Canvas init start"
sudo update-rc.d canvas_init defaults
sudo /etc/init.d/canvas_init start
echo "Restart apache2"
sudo /etc/init.d/apache2 restart
echo "START RAILS SERVER"
bundle exec rails server &
